package com.ytc.content.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ytc.basic.api.BasicClient;
import com.ytc.commons.constants.CommonConstants;
import com.ytc.commons.constants.CourseConstants;
import com.ytc.commons.model.vo.CategoryVo;
import com.ytc.commons.model.vo.ResponseResult;
import com.ytc.commons.model.vo.TeachplanVo;
import com.ytc.commons.utils.BeanHelper;
import com.ytc.content.config.MinioProperties;
import com.ytc.content.domain.CourseBase;
import com.ytc.content.domain.CoursePub;
import com.ytc.content.domain.CourseTeacher;
import com.ytc.content.mappers.CoursePubMapper;
import com.ytc.content.service.CourseBaseService;
import com.ytc.content.service.CoursePubService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ytc.content.service.CourseTeacherService;
import com.ytc.content.service.TeachplanService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.minio.PutObjectArgs;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * <p>
 * 课程发布 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-01
 */
@Service
public class CoursePubServiceImpl extends ServiceImpl<CoursePubMapper, CoursePub> implements CoursePubService {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private TeachplanService teachplanService;
    @Autowired
    private CourseTeacherService courseTeacherService;
    @Autowired
    private BasicClient basicClient;

    //课程预览
    @Override
    public Map<String, Object> preview(Long courseBaseId) {
        //查询课程数据
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        //课程计划数据
        ResponseResult<TeachplanVo> treeNodes = teachplanService.treeNodes(courseBaseId);
        TeachplanVo vo = treeNodes.getData();
        //课程讲师数据
        ResponseResult<List<CourseTeacher>> listTeacher = courseTeacherService.listTeacher(courseBaseId);
        //feign,找分类名称
         String stName = this.getCategoryName(courseBase.getSt());
         String mtName = this.getCategoryName(courseBase.getMt());
        //构造CoursePub对象
        CoursePub pub = new CoursePub();
        pub = BeanHelper.copyProperties(courseBase,CoursePub.class);
        pub.setId(null);
        pub.setCourseId(courseBase.getId());
        //设置分类
        pub.setStName(stName);
        pub.setMtName(mtName);
        //设置课程计划
        pub.setTeachplan(JSON.toJSONString(vo));
        //设置讲师
        pub.setTeachers(JSON.toJSONString(listTeacher));
        //返回map集合
        Map<String,Object> map = new HashMap<>();
        map.put("coursePub",pub);
        map.put("teachplanNode",vo);


        return map;
    }

    @Autowired
    private Configuration configuration;
    @Autowired
    private MinioProperties properties;
    @Autowired
    private PublishHtmlService htmlService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //课程发布
    @Override
    public ResponseResult publishCourse(Long courseBaseId) throws Exception {
        //1.根据课程id查询课程
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        //2.构造课程发布对象
        Map<String,Object> map = this.preview(courseBaseId);
        CoursePub coursePub = (CoursePub) map.get("coursePub");
        //3.根据课程id查询此课程是否发布过
        LambdaQueryWrapper<CoursePub> qw = new LambdaQueryWrapper<>();
        qw.eq(CoursePub::getCourseId,courseBaseId);
        CoursePub one = this.getOne(qw);
        if(one==null){
            this.save(coursePub);
        }else { //如果存在 更新
            coursePub.setId(one.getId());
            this.updateById(coursePub);
        }
        //4.更新课程状态(课程表里面的状态)
        courseBase.setAuditStatus(CourseConstants.AUDIT_PUBLISHED_STATUS);
        courseBaseService.updateById(courseBase);

        //5.调用PublishHtmlService完成页面静态化
         htmlService.publish(map,coursePub.getId());

         //6.发送rabbitmq消息
        rabbitTemplate.convertAndSend("content-exchange","course.pub",coursePub.getId());
        //（主题模式）三个参数交换机名 路由key，消息内容


        return ResponseResult.okResult();
    }


    /*
    * 获取分类名称的方法
    * */
    private String getCategoryName(String id){
        ResponseResult<CategoryVo> result = basicClient.findById(id);
        CategoryVo data = result.getData();
        return data.getName();
    }
}
